#include <QDate>
#include <QTableWidgetItem>
#include <QStandardItemModel>

#include "operationswidget.h"
#include "ui_operationswidget.h"
#include "datedelegate.h"
#include "floatdelegate.h"
#include "comboboxdelegate.h"
//#include "locationdelegate.h"
//#include "positiondelegate.h"
#include "logger.h"
#include "defs.h"
#include "manager.h"

OperationsWidget::OperationsWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::OperationsWidget)
{
    ui->setupUi(this);
}

OperationsWidget::~OperationsWidget()
{
    delete ui;
}

void OperationsWidget::setup()
{
    setupModel();
    setupView();

    connect(ui->btnAdd, SIGNAL(clicked()),
            App->mainWindow(), SLOT(markAsModified()));
    connect(ui->btnDel, SIGNAL(clicked()),
            App->mainWindow(), SLOT(markAsModified()));

    ui->btnDel->setEnabled(false);
}

void OperationsWidget::clearAll()
{
    ui->btnDel->setEnabled(false);
}

void OperationsWidget::setupModel()
{
    if (App->pacient()->operations()==NULL)
    {
        LOG(objectName(), "App->pacient()->operations()==NULL");
    } else {
        ui->tblOperations->setModel( App->pacient()->operations() );
    }
}

void OperationsWidget::setupView()
{
    QTableView *tw = ui->tblOperations;
    tw->horizontalHeader()->setStretchLastSection(true);

    tw->setItemDelegateForColumn(OC_Date, new DateDelegate(this));
    tw->setItemDelegateForColumn(OC_Age, new FloatDelegate(this));
    tw->setItemDelegateForColumn(OC_Limb, new ComboBoxDelegate(LimbStrings, this) );
    tw->setItemDelegateForColumn(OC_Bone, new ComboBoxDelegate(OperationBones, this));
    tw->setItemDelegateForColumn(OC_Location, new ComboBoxDelegate(OperationLocations, this));
    tw->setItemDelegateForColumn(OC_Method, new ComboBoxDelegate(App->mOperationTypes, this));

    tw->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
}

void OperationsWidget::setEnabledBtnDel()
{
    ui->btnDel->setEnabled(true);
}

void OperationsWidget::addLine(const QVariantList &vl)
{
    const int newRowCount = App->pacient()->operations()->rowCount();
    App->pacient()->addOperation( vl );
    ui->tblOperations->selectRow( newRowCount );
    ui->btnDel->setEnabled( true );
}

void OperationsWidget::delLine(const int rowNumber)
{
    if (rowNumber>=0 && rowNumber < App->pacient()->operations()->rowCount())
    {
        App->pacient()->operations()->removeRow(rowNumber);
        if (rowNumber>0)
            ui->tblOperations->selectRow(rowNumber - 1);
    }
}

void OperationsWidget::on_btnAdd_clicked()
{
    QDate curDate(QDate::currentDate());
    QVariantList vl;
    for (int i=0; i<OperationColCount; ++i)
        vl.append(QString(""));
    vl[OC_Date] = curDate;
    vl[OC_Age] = App->pacient()->ageAtDate(curDate);
    vl[OC_Limb] = 0;
    vl[OC_Bone] = 0;
    vl[OC_Location] = 0;
    vl[OC_Method] = 0;

    addLine( vl );
    ui->btnDel->setEnabled(true);
}

void OperationsWidget::on_btnDel_clicked()
{
    const int curRowNum = ui->tblOperations->currentIndex().row();
    delLine(curRowNum);
    if (App->pacient()->operations()->rowCount() == 0)
        ui->btnDel->setEnabled(false);
}
